﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ServiceBase
{
    /// <summary>
    /// 判断某像素的颜色
    /// </summary>
    public static class IsAnyColor
    {
        #region 黑
        /// <summary>
        /// 是否是黑色
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool IsBlack(Color c)
        {
            return (IsBlackByB(c) || IsBlackByArray(c) || IsBlackByArea(c));
        }
        /// <summary>
        /// 根据颜色的Blue值判断是否为黑色
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private static bool IsBlackByB(Color c)
        {
            if ((float)c.B < 70)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 使用颜色数字区间来判断此颜色是否是黑色
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private static bool IsBlackByArea(Color c)
        {
            //标准黑色
            int black = -16777216;
            //误差
            //左偏量
            int lMiss = 2000;
            //右偏量
            int rMiss = 2000;
            int left = black - lMiss;
            int right = black + rMiss;
            if (c.ToArgb() > left && c.ToArgb() < right)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 使用预置黑色数组来判断此颜色是否是黑色
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private static bool IsBlackByArray(Color c)
        {
            string[] blacks = new string[] { 
                "#424242",
                "#000000",
                "#020202",
                "#101010",
                "#3E3E3E",
                "#333333",
                "#0B0B0B",
                "#3A3A3A",
                "#3F3F3F",
                "#48484A",
                "#AAAAAA",
                "#A8A8A8",
                "#353535",
                "#A5A5A5",
                "#AAAAAA",
                "#0A0A0A",
                "#0D0D0D",
                "#434343",
                "#444444",
                "#010005",
                "#000100",
                "#0C0C0C"
            };
            foreach (string var in blacks)
            {
                if (ColorTranslator.ToHtml(c) == var)
                {
                    return true;
                }
            }
            return false;
        } 
        #endregion

        #region 蓝
        /// <summary>
        /// 是否是蓝色
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool IsBlue(Color c)
        {
            if ((float)c.B > 70 && c != Color.White)
            {
                return true;
            }
            return false;
        } 
        #endregion
    }
}
